<?php
namespace Mobile\Controller;
use Think\Controller;
class TestController extends Controller {


    function __construct()
    {
        parent::__construct();
        $this->M_order = M('order');
        $this->M_goods = M('Goods');
        $this->M_address = M('address');
        $this->M_goods_spec_option=M('goods_spec_option');
    }


    public function order(){
        $order_list=M('order')->where('order_id=14')->select();
        $option_detail=unserialize($order_list[0]['option_detail']);
        var_dump($option_detail);die;
    }

    /**
     *  创建订单
     */
//    public function add_order(){
//        $order_list=I('post.');
//        $order_sn=I('post.order_sn');
//        $address_id=I('post.address_id');
////        $order_list=array(
////                '0'=>array(
////                    '0'=>40,//商品id
////                    '1'=>63,//option的id
////                    '2'=>3//购买数量
////            ),
////                '1'=>array(
////                    '0'=>37,
////                    '1'=>53,
////                    '2'=>3
////                ),
////        );
//        $data['user_id'] = $this->u_info['user_id'];//用户id
//        $data['order_sn']=$order_sn;
//        $user_address=$this->M_address->where("address_id='".$address_id."'")->find();
//        if($user_address) {
//            $data['full_name'] = $user_address['full_name'];
//            $data['address'] = $user_address['area'] . $user_address['details'];
//            $data['tel'] = $user_address['tel'];
//            $data['c_id'] = I('post.cid');
//            $data['add_time'] = time();
//            $data['status'] = 1;
//            $all_price = 0;
//            foreach ($order_list as $key => $value) {
//                $goods_id[$key] = $value['goods_id'];
//                $order_option = $this->M_goods_spec_option->where("goods_option_id='" . $value['option_detail'][1] . "'")->find();
//                if ($order_option['stock'] <= 0) {
//                    echo json_encode(array('errMsg' => '没有库存了!'));
//                    exit;
//                }
//                $all_price += floatval($order_option['price']) * $value['option_detail'][2];
//                if (!empty($value['option_detail'])) {
//                    $option_detail[$key] = $value['option_detail'];
//                }
//            }
//            $data['goods_id'] = ltrim(implode(',', $goods_id), ',');
//            $data['option_detail'] = serialize($option_detail);
//            $data['total_price'] = $all_price;
//            $res = $this->M_order->add($data);
//            if ($res) {
//                echo json_encode(array('status'=>1,'errMsg' => '创建订单成功'));
//            } else {
//                echo json_encode(array('status'=>0,'errMsg' => '创建订单失败'));
//            }
//        }else{
//            echo json_encode(array('errMsg'=>'您还没有添加收货地址!'));
//        }
//    }
    public function test_order(){
//        $order_list=I('post.');
        $order_list=array(
            '0'=>array(
                'goods_id'=>40,
                'option_detail'=>array(
                    '0'=>40,//商品id
                    '1'=>63,//option的id
                    '2'=>3//购买数量
                ),
            ),
            '1'=>array(
                'goods_id'=>37,
                'option_detail'=>array(
                    '0'=>37,
                    '1'=>53,
                    '2'=>3
                ),
            ),
        );
        $data['user_id'] = 3;//用户id
        $user_address=$this->M_address->where("user_id='".$data['user_id']."'")->find();
        $data['full_name']=$user_address['full_name'];
        $data['address']=$user_address['area'].$user_address['details'];
        $data['tel']=$user_address['tel'];
        $data['c_id']=3;
        $data['add_time']=time();
        $data['status']=1;
        $all_price=0;
        foreach($order_list as $key=>$value){
            $goods_id[$key]=$value['goods_id'];
            $order_option=$this->M_goods_spec_option->where("goods_option_id='".$value['option_detail'][1]."'")->find();
            if($order_option['stock']<=0){
//                    echo json_encode(array('errMsg'=>'没有库存了!'));
                echo"没有库存了";
                exit;
            }
            $all_price+=floatval($order_option['price'])*$value['option_detail'][2];
            if(!empty($value['option_detail'])){
                $option_detail[$key]=$value['option_detail'];
            }
        }
        $data['goods_id']=ltrim(implode($goods_id,','),',');
        $data['option_detail']=serialize($option_detail);
        $data['total_price']=$all_price;
        $res=$this->M_order->add($data);
        if($res){
//            echo json_encode(array('errMsg'=>'创建订单成功'));
            echo"创建订单成功";
        }else{
//            echo json_encode(array('errMsg'=>'创建订单失败'));
            echo"创建订单失败";
        }
    }
    public function is_pay(){
        $order_id=127;
        $data['status']=2;
        $res=$this->M_order->where("order_id='".$order_id."'")->save($data);
        if($res){
            $order_list=$this->M_order->where("order_id='".$order_id."'")->find();
            $goods_id=explode(',',$order_list['goods_id']);
            $option_detail=unserialize($order_list['option_detail']);
            $this->M_goods_spec_option->startTrans();
            foreach($goods_id as $k=>$v){
                foreach($option_detail as $key=>$value){
                    if($v==$value[0]){
                        $order_option=$this->M_goods_spec_option->where("goods_option_id='".$value[1]."'")->find();
                        $option['stock']=$order_option['stock']-$value[2];
                        $o_res=$this->M_goods_spec_option->where("goods_option_id='".$value[1]."'")->save($option);
                        if($o_res){
                            $flag=1;
                        }else{
                            $flag=0;
                        }
                    }
                }
            }
            if($flag==1){
                $this->M_goods_spec_option->commit();
                echo json_encode(array('errMsg'=>'修改库存成功!'));
            }else{
                $this->sorder->rollback();
                echo json_encode(array('errMsg'=>'修改库存失败!'));
            }
            echo json_encode(array('errMsg'=>'已完成支付!'));
        }else{
            echo json_encode(array('errMsg'=>'支付失败,请联系客服!'));
        }
    }
    public function edit_address(){
        $id = 11;
        if($id){
            $info = $this->M_address->where('user_id = 2880 AND address_id = '.$id)->find();
            if(empty($info))
                $this->re_json(array('error'=>'系统错误！'));
        }
        $this->M_address->full_name = I('post.full_name');
        if(I('post.full_name') =='')
            $this->re_json(array('errMsg'=>'收件人不能为空'));
        $this->M_address->area = I('post.area');
        if(I('post.area') =='')
            $this->re_json(array('errMsg'=>'收件地区不能为空'));
        $this->M_address->details = I('post.details');
        if(I('post.details') =='')
            $this->re_json(array('errMsg'=>'收件详细地址不能为空'));
        $this->M_address->tel = I('post.tel');
        if(I('post.tel') =='')
            $this->re_json(array('errMsg'=>'电话号码不能为空'));
        $this->M_address->user_id = $this->uid['user_id'];
        $this->M_address->zip_code = I('post.zip_code');
        if($info){
            $this->M_address->save()?$this->re_json(array('errMsg'=>'更新成功')):$this->re_json(array('errMsg'=>'更新失败'));
        }else{
            $this->M_address->add()?$this->re_json(array('errMsg'=>'添加成功')):$this->re_json(array('errMsg'=>'添加失败'));
        }
    }
    public function pay(){
        include_once('./wxpay/wxpay.php');
        $this->c_info = M('company')->find(3);
        $appid=$this->c_info['appid'];
        $openid='o5soM0fUvUT9KqWaSogPCmDBD0-o';
        $mch_id ='1369479302';//商户号
        $key='DF806C9DCB3B16A9791A58AF44A02157';//密钥
        $out_trade_no =$mch_id.time().'';
        $total_fee = '0.01';//金额
        if(empty($total_fee)) //押金
        {
            $body = "充值押金";
            $total_fee = floatval(99*100);
        }
        else {
            $body = "充值余额";
            $total_fee = floatval($total_fee*100);
        }
//        echo $appid.'<br/>.'.$openid.'<br/>.'.$mch_id.'<br/>.'.$key.'<br/>.'.$out_trade_no.'<br/>.'.$body.'<br/>.'.$total_fee;
//        exit;
        $weixinpay = new \WeixinPay($appid,$openid,$mch_id,$key,$out_trade_no,$body,$total_fee);
        $return=$weixinpay->pay();
        echo json_encode($return);
    }
}
